package com.google.gwt.sample.hello.client.notgenerated.eventFramework.controler;

import com.google.gwt.sample.hello.client.notgenerated.eventFramework.Accessor;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.Property;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.PropertyChangeEvent;
import com.google.gwt.sample.hello.client.notgenerated.eventFramework.PropertyChangeListener;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.KeyboardListener;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

@SuppressWarnings("deprecation")
public class StringToTextField implements PropertyChangeListener<String>,
		ChangeListener, KeyboardListener {

	private TextBox field;
	Accessor<String> accessor;

	public StringToTextField(TextBox f, Property<String> p) {
		this.accessor = p.getAccessor();
		this.field = f;
		field.setText(accessor.get());
		f.addKeyboardListener(this);
		f.addChangeListener(this);
		p.addPropertyChangeListener(this);
		
	}

	public void onChange(Widget sender) {
		accessor.set(((TextBox) sender).getText());
	}

	public void propertyChanged(PropertyChangeEvent<String> evt) {
		if (!field.getText().equals(evt.getNewValue())) {
			field.setText((String) evt.getNewValue());
		}
	}

	public void onKeyDown(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyPress(Widget sender, char keyCode, int modifiers) {

	}

	public void onKeyUp(Widget sender, char keyCode, int modifiers) {
		accessor.set(field.getText());
	}

}
